package com.itbaizhan.openhis.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbaizhan.openhis.constants.Constants;
import com.itbaizhan.openhis.domain.Dept;
import com.itbaizhan.openhis.domain.User;
import com.itbaizhan.openhis.dto.UserDto;
import com.itbaizhan.openhis.mapper.UserMapper;
import com.itbaizhan.openhis.service.UserService;
import com.itbaizhan.openhis.utils.AppMd5Utils;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author MR
 * @description 针对表【sys_user(用户信息表)】的数据库操作Service实现
 * @createDate 2025-05-16 15:16:39
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
		implements UserService {
	
	@Autowired
	private UserMapper userMapper;
	
	/**
	 * 根据手机号码查询用户
	 *
	 * @param phone
	 * @return
	 */
	@Override
	public User queryUserByPhone(String phone) {
		QueryWrapper<User> wrapper = new QueryWrapper<>();
		wrapper.eq(User.COL_PHONE, phone);
		return userMapper.selectOne(wrapper);
	}
	
	/**
	 * 分页查询用户
	 *
	 * @param userDto
	 * @return
	 */
	@Override
	public DataGridView listUserForPage(UserDto userDto) {
		Page<User> page = new Page<>(userDto.getPageNum(), userDto.getPageSize());
		QueryWrapper<User> wrapper = new QueryWrapper<>();
		wrapper.like(StringUtils.isNotBlank(userDto.getUserName()),
				User.COL_USER_NAME, userDto.getUserName());
		wrapper.like(StringUtils.isNotBlank(userDto.getPhone()),
				User.COL_PHONE, userDto.getPhone());
		wrapper.eq(StringUtils.isNotBlank(userDto.getStatus()),
				User.COL_STATUS, userDto.getStatus());
		wrapper.eq(userDto.getDeptId() != null,
				User.COL_DEPT_ID, userDto.getDeptId());
		wrapper.ge(userDto.getBeginTime() != null,
				User.COL_CREATE_TIME, userDto.getBeginTime());
		wrapper.le(userDto.getEndTime() != null,
				User.COL_CREATE_TIME, userDto.getEndTime());
		wrapper.orderByAsc(User.COL_USER_ID);
		userMapper.selectPage(page, wrapper);
		return new DataGridView(page.getTotal(), page.getRecords());
	}
	
	/**
	 * 添加用户
	 *
	 * @param userDto
	 * @return
	 */
	@Override
	public int addUser(UserDto userDto) {
		User user = new User();
		BeanUtils.copyProperties(userDto, user);
		user.setUserType(Constants.USER_NORMAL);
		user.setCreateBy(userDto.getSimpleUser().getUserName());
		user.setCreateTime(DateUtil.date());
		//设置默认密码为手机后5位
		String defaultPwd = user.getPhone().substring(5);
		user.setSalt(AppMd5Utils.md5(defaultPwd, user.getSalt(), 2));
		return userMapper.insert(user);
	}
	
	/**
	 * 修改用户
	 *
	 * @param userDto
	 * @return
	 */
	@Override
	public int updateUser(UserDto userDto) {
		User user = userMapper.selectById(userDto.getUserId());
		if (user == null) {
			return 0;
		}
		BeanUtils.copyProperties(userDto, user);
		user.setUpdateBy(userDto.getSimpleUser().getUserName());
		user.setUpdateTime(DateUtil.date());
		return userMapper.updateById(user);
	}
	
	/**
	 * 批量删除用户
	 * @param userIds
	 * @return
	 */
	@Override
	public int deleteUserByIds(Long[] userIds) {
		List<Long> ids = Arrays.asList(userIds);
		if (ids != null && ids.size() > 0) {
			return userMapper.deleteBatchIds(ids);
		} else {
			return 0;
		}
	}
	
	/**
	 * 重置密码
	 * @param userIds
	 */
	@Override
	public void resetPwd(Long[] userIds) {
		for (Long userId : userIds) {
			User user = userMapper.selectById(userId);
			String defaultPwd = "";
			if (user.getUserType().equals(Constants.USER_ADMIN)) {
				defaultPwd = "123456";
			}else{
				defaultPwd = user.getPhone().substring(5);
			}
			//随机盐zhi
			user.setSalt(AppMd5Utils.createSalt());
			//设置密码为defaultPwd
			user.setPassword(AppMd5Utils.md5(defaultPwd, user.getSalt(), 2));
			userMapper.updateById(user);
		}
	}
	
	/**
	 * 查询用户信息
	 * @param userId
	 * @return
	 */
	@Override
	public User getUserById(Long userId) {
	 return userMapper.selectById(userId);
	}
}




